<?php

namespace Apps\Frontend\Controllers;
use Apps\Frontend\Models\City;
use Apps\Frontend\Models\District;
use Apps\Frontend\Models\Shop;
use Apps\Frontend\Models\Vouchers;

class IndexController extends \Phalcon\Mvc\Controller{

    public function initialize(){
        $keyCache = 'city_list';
        $tbResult = $this->viewCache->get($keyCache,86400);
        if(empty($tbResult)){
            $tbCity = City::find(array('status = 1','order'=>'orders DESC','columns'=>'provinceid,name,type,price'));
            $tbShop = Shop::find(array('status = 1','order'=>'id ASC','columns'=>'id,name,kinhdo,vido,address,phone,province_id,district_id,website'));
            $tbResult = array('city'=>$tbCity,'shop'=>$tbShop);
            $this->viewCache->save($keyCache,$tbResult);
        }

        $this->view->results = $tbResult;

    }
    public function indexAction(){
        if($this->request->isPost()){
            $data = $this->request->getPost();
            if($this->check($data['email'],$data['phone'])):
                $code = $this->code();
                $voucher = new Vouchers();
                $voucher->code          =   $code;
                $voucher->name          =   $data['name'];
                $voucher->phone         =   $data['phone'];
                $voucher->email         =   $data['email'];
                $voucher->sms           =   0;
                $voucher->status        =   0;
                $voucher->ip            =   $data['ip'];
                $voucher->country       =   $data['country'];
                $voucher->city          =   $data['city'];
                $voucher->location      =   $data['location'];
                $voucher->lat           =   $data['lat'];
                $voucher->lon           =   $data['lon'];
                $voucher->shop_id       =   $data['shop_id'];
                $voucher->created       =   time();
                $voucher->finished      =   0;
                $voucher->modified_by   =   0;
                
               /* if ($voucher->save() == false) {
                echo "Umh, We can't store robots right now: \n";
                foreach ($voucher->getMessages() as $message) {
                        echo $message, "\n";
                    }
                } else {
                    echo "Great, a new robot was saved successfully!";
                }
                */
            endif;
        }
    }
    
     private function code(){
        $code = rand(0,99999);
        $code = md5(md5($code));
        $code = substr($code,0,8);
        $vcode = Vouchers::find("code = '{$code}'")->toArray();
        if(count($vcode) > 0){
            $this->code();    
        }else{
           return strtoupper($code); 
        }
     } 
     
     private function check($email,$phone){
        $v = Vouchers::find("email = '{$email}' OR phone = '{$phone}'")->toArray();
        if(count($v) > 0){
            return false;            
        }else{
            return true; 
        }
     } 
}